package com.xiaoai.juc;

/**
 * @Author
 * @Date 2021-09-20 20:00
 */
public class SaleTicketDemo01 {

    public static void main(String[] args){

        Ticket1 ticket1 = new Ticket1();

        new Thread(() -> {
            for (int i = 0; i < 30; i++) {
                ticket1.sale();
            }
        }).start();

        new Thread(() -> {
            for (int i = 0; i < 30; i++) {
                ticket1.sale();
            }
        }).start();

        new Thread(() -> {
            for (int i = 0; i < 30; i++) {
                ticket1.sale();
            }
        }).start();

    }

}

class Ticket1{
    private int number = 30;

    public synchronized void sale(){
        if (number > 0){
            System.out.println(Thread.currentThread().getName()+"卖出第["+number--+"]张票，剩余:["+number+"]张");
        }
    }
}
